#include <func.h>
typedef struct myMsg
{
    long mtype;
    char mtext[512];
} myMsg_t;
int display();

int main()
{

    int ret = display();
    ERROR_CHECK(ret, -1, "display");

    return 0;
}

int display()
{
    printf("==============================B1================\n");
    while (1)
    {
        int msqid = msgget(2000, IPC_CREAT | 0600);
        ;
        ERROR_CHECK(msqid, -1, "msgget");
        myMsg_t msgfromb;
        memset(&msgfromb, 0, sizeof(msgfromb));
        msgrcv(msqid, &msgfromb, sizeof(msgfromb), 1, 0);
        int type = 0;
        sscanf(msgfromb.mtext, "%*d %d", &type);
        switch (type)
        {
        case 1:
        {
            printf("<<<<<<<<<<<<<<<<<<receive<<<<<<<<<<<<<<<<<\n");
            struct tm *ptm = NULL;
            time_t tmp = 0;
            char ss[512] = {0};
            sscanf(msgfromb.mtext, "%ld", &tmp);
            sscanf(msgfromb.mtext, "%*d %*d %[^\n]", ss);
            ptm = gmtime(&tmp);
            printf("%4d-%02d-%02d %02d:%02d:%02d\n",
                   ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
                   ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
            puts(ss);
            printf("\n");
            // 清空共享内存中的数据
            break;
        }
        case 2:
        {
            // 删除消息队列并退出
            printf("Bye Bye.\n");
            msgctl(msqid, IPC_RMID, NULL);
            exit(0);
        }
        case 3:
        {
            printf(">>>>>>>>>  send  >>>>>>>>>>>\n");
            struct tm *ptm3 = NULL;
            time_t tmp3 = 0;
            char ss3[512] = {0};
            sscanf(msgfromb.mtext, "%ld", &tmp3); // 读取消息中的时间信息
            sscanf(msgfromb.mtext, "%*d %*d %[^\n]", ss3);
            ptm3 = gmtime(&tmp3);
            printf("%4d-%02d-%02d %02d:%02d:%02d\n",
                   ptm3->tm_year + 1900, ptm3->tm_mon + 1, ptm3->tm_mday,
                   ptm3->tm_hour, ptm3->tm_min, ptm3->tm_sec);
            puts(ss3);
            printf("\n");
            break;
        }
        default:
        {
            printf("Something wrong!\n");
        }
        }
    }
}
